package com.se.nju.rtvps.service;

import java.io.IOException;

import com.se.nju.rtvps.main.StartUp;
import com.se.nju.rtvps.network.RTVPSPeerGroup;
import com.se.nju.rtvps.network.PipeAdvFactory;

import net.jxta.discovery.DiscoveryService;
import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.exception.PeerGroupException;
import net.jxta.id.ID;
import net.jxta.id.IDFactory;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeID;
import net.jxta.platform.Module;
import net.jxta.platform.ModuleClassID;
import net.jxta.protocol.ModuleClassAdvertisement;
import net.jxta.protocol.ModuleSpecAdvertisement;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.service.Service;
import net.jxta.util.JxtaServerPipe;

public class ControlService implements net.jxta.service.Service{
	private Advertisement implAdv;
	private PipeAdvertisement pipeAdv;
	private Thread opThread;
	private PeerGroup group;

	public Advertisement getImplAdvertisement() {
		return implAdv;
	}

	public Service getInterface() {
		return this;
	}

	public void init(PeerGroup group, ID assignedID, Advertisement implAdv) throws PeerGroupException {
		System.out.println("Init Control Service");
		this.implAdv = implAdv;
		this.group = group;
		try {
			this.pipeAdv = PipeAdvFactory.getPipeAdvService().getControlServicePipeAdv();
			System.out.println(pipeAdv);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void registerOperation(OperationFactory opFactory) {
		// TODO Auto-generated method stub
		
	}

	public int startApp(String[] args) {
		System.out.println("Start Control Service");
		OperationFactory opFactory = new OperationFactory();
		registerOperation(opFactory);
		Thread opThread = new Thread(new ControlServiceThread(StartUp.getRTVPSPeerGroup().getNetPeerGroup(),pipeAdv,opFactory));
		opThread.start();
		return Module.START_OK;
	}

	public void stopApp() {
		System.out.println("Stop Control Service");
		opThread.stop();
	}
}
